//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by celeste on 16/5/10.
//  Copyright © 2016年 celeste. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        setupTabbar()
        
        showNewFeature()
        
    }
    
    @objc private func clickComposeButton(){
    
        print(__FUNCTION__)
    
    }
    
    // MARK: - 私有控件
    /// 撰写按钮
    
    private lazy var composeButton: UIButton = UIButton(
        ld_title: nil,
        imageName: "tabbar_compose_icon_add",
        backgroundImageName: "tabbar_compose_button")
  
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        setupComposeButton()
    }
    
}
// MARK : - 新特性处理
extension MainViewController {
    
    /// 显示新特性
    private func showNewFeature() {
        //1.如果没有登录直接返回
        if !UserAccount.sharedUserAccount.isLogin {
            return
        }
        //2.判断是否有新版本
        let v = isNewVersion ? NewFeatureView() : WelcomeView()
        //3.将试图加到当前试图
        view .addSubview(v)
        
        v.frame = view.bounds
    }
    private var isNewVersion: Bool {
        
        let currentVersion = NSBundle.ld_currentVersion
        
        let versionKey = "ld.versionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
        
        return currentVersion != sandboxVersion
    }
}

// MARK : - 添加子控制器
extension MainViewController{
    
    ///添加所有子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        //添加一个空的控制器站位
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    ///添加子控制器的方法
    
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont .systemFontOfSize(10)], forState: .Normal)
        
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
}

// // MARK : - 设置tabbar
extension MainViewController{
    
    //设置tabbar
    private func setupTabbar(){
        
        tabBar.shadowImage = UIImage.ld_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    }
    
    // 设置撰写按钮位置
    
    private func setupComposeButton(){
        
        tabBar.bringSubviewToFront(composeButton)
        
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
    }
    
    
    
    
    
}









